package com.example.server;


import com.example.protocol.MessageCodecSharable;
import com.example.protocol.ProtocolFrameDecoder;
import com.example.server.handler.ChatRequestMessageHandler;
import com.example.server.handler.GroupChatRequestMessageHandler;
import com.example.server.handler.GroupCreateRequestMessageHandler;
import com.example.server.handler.GroupJoinRequestMessageHandler;
import com.example.server.handler.GroupMembersRequestMessageHandler;
import com.example.server.handler.GroupQuitRequestMessageHandler;
import com.example.server.handler.LoginRequestMessageHandler;
import com.example.server.handler.QuitHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ChatServer {
    public static void main(String[] args) {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();
        LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
        MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.group(boss, worker);
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ProtocolFrameDecoder());
                    ch.pipeline().addLast(LOGGING_HANDLER);
                    ch.pipeline().addLast(MESSAGE_CODEC);

                    // 空闲事件检测
                    ch.pipeline().addLast(new IdleStateHandler(5, 0, 0));
                    // 关注上面产生的事件
                    ch.pipeline().addLast(new ChannelDuplexHandler() {
                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                            // 用来触发特殊事件
                            IdleStateEvent event = (IdleStateEvent) evt;
                            // 触发了读空闲事件
                            if (event.state() == IdleState.READER_IDLE) {
                                // 空闲了 可考虑断开
                                log.debug("已经五秒没有读到数据了。");
                                ctx.channel().close();
                            }
                        }
                    });
                    // 只关心LoginRequestMessage消息
                    ch.pipeline().addLast(new LoginRequestMessageHandler());
                    // 只关心ChatRequestMessage消息
                    ch.pipeline().addLast(new ChatRequestMessageHandler());
                    // 只关心GroupCreateRequestMessage消息
                    ch.pipeline().addLast(new GroupCreateRequestMessageHandler());
                    // 只关心GroupChatRequestMessage消息
                    ch.pipeline().addLast(new GroupChatRequestMessageHandler());
                    //只关心GroupJoinRequestMessage消息
                    ch.pipeline().addLast(new GroupJoinRequestMessageHandler());
                    // 只关心GroupMembersRequestMessage消息
                    ch.pipeline().addLast(new GroupMembersRequestMessageHandler());
                    // 只关心GroupQuitRequestMessage消息
                    ch.pipeline().addLast(new GroupQuitRequestMessageHandler());
                    ch.pipeline().addLast(new QuitHandler());
                }
            });
            Channel channel = serverBootstrap.bind(8080).sync().channel();
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            log.error("server error", e);
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}
